package com.kaigejava.springwebsocket.controller.tomcatwebscoket;

import com.alibaba.fastjson.JSON;
import com.kaigejava.springwebsocket.utils.ChatUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.*;

@Controller
@Slf4j
public class TomcatChatContract {

    @Autowired
    private ChatUserUtil chatUserUtil;

    @RequestMapping("/onlineusers")
    @ResponseBody
    public Map<String,Object> onlineusers(HttpSession httpSession){
        Map<String,Object> rMap = new HashMap<>();
        Map<String, Session> map=chatUserUtil.userSocketSessionMap;
        Set<String> set=map.keySet();
        Iterator<String> it = set.iterator();
        Set<String> nameset=new HashSet<String>();
        while(it.hasNext()){
            String entry = it.next();
            String httpUserNmae = (String)httpSession.getAttribute("userName");
            log.info("===entry:{},httpUserNmae:{}", JSON.toJSONString(entry),JSON.toJSONString(httpUserNmae));
            if(!entry.equals(httpUserNmae)){
                nameset.add(entry);
            }
            rMap.put("userName",entry);
        }


        return rMap;
    }
}
